﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TimKen.App;
using TimKen.BussLogic;

namespace TimKen.Web.Admin.Customer
{
    public partial class Customer_Info : TimKen.App.AdminPage
    {
        private CustomerManager ctm = new CustomerManager();
        public override string ActionCode
        {
            get
            {
                return "92";//和客户列表权限相同
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindResources();
                if (Request.QueryString["CustomerID"] != null && TypeParse.IsNumeric(Request.QueryString["CustomerID"]))
                {
                    CustomerID = Convert.ToInt32(Request.QueryString["CustomerID"]);
                }
                BindData();
            }
        }

        private void BindData()
        {
            TimKen.DataEntity.Customer customer = ctm.GetModel(CustomerID);
            if (customer != null)
            {
                labCustomerID.Value = customer.CustomerID.ToString();
                labAddress.Text = customer.Address;
                labCardCode.Text = customer.CardCode;
                labCity.Text = customer.City;
                labContactPerson.Text = customer.ContactPerson;
                labCountry.Text = customer.Country;
                labCounty.Text = customer.County;
                labCreditLimit.Text = customer.CreditLimit.ToString();
                labCurrency.Text = customer.Currency;
                labCurrentAccountBalance.Text = customer.CurrentAccountBalance.ToString();
                labCustomerForeignName.Text = customer.CustomerForeignName;
                labCustomerName.Text = customer.CustomerName;
                labDescription.Text = customer.Description;
                labDiscountPercent.Text = customer.DiscountPercent.ToString();
                labECOrdersBalance.Text = customer.ECOrdersBalance.ToString();
                labEmail.Text = customer.Email;
                labFax.Text = customer.Fax;
                labFrozen.Text = GetBoolPic(customer.Frozen);
                labFrozenFrom.Text = customer.FrozenFrom == null ? "" : customer.FrozenFrom.Value.ToShortDateString();   ///////////////
                labFrozenRemarks.Text = customer.FrozenRemarks;
                labFrozenTo.Text = customer.FrozenTo == null ? "" : customer.FrozenTo.Value.ToShortDateString();
                labLanguageID.Text = customer.LanguageID.ToString();
                labMobile.Text = customer.Mobile;
                labNotes.Text = customer.Notes;
                labProvince.Text = customer.Province;
                labSalesPersonCode.Text = customer.SalesPersonCode.ToString();
                labShippingType.Text = customer.ShippingType;
                labShipToDefault.Text = customer.ShipToDefault;
                labTaxClassID.Text = customer.TaxClassID.ToString();
                labTaxRate.Text = customer.TaxRate.ToString();
                labTelephone.Text = customer.Telephone;
                labValid.Text = GetBoolPic(customer.Valid);
                labValidFrom.Text = customer.ValidFrom.ToString();
                labValidRemarks.Text = customer.ValidRemarks;
                labValidTo.Text = customer.ValidTo.ToString();
                BindGrid(customer);
            }

        }

        protected void BindGrid(TimKen.DataEntity.Customer customer)
        {
            //绑定客户的用户列表
            TimKen.BussLogic.UserAccountManager userM = new TimKen.BussLogic.UserAccountManager();
            grdUserAccountList.DataSource = userM.GetUserAccountPageList(AspNetPager1.PageSize, AspNetPager1.CurrentPageIndex, "-1", CustomerID.ToString(), "", LanguageID);
            grdUserAccountList.DataBind();

            //绑定客户的地址
            AddressManager addressM = new AddressManager();
            List<TimKen.DataEntity.Address> BillDs = addressM.GetBillToAddress(customer.CardCode);
            List<TimKen.DataEntity.Address> ShipDs = addressM.GetShipToAddress(customer.CardCode);
            grdShipList.DataSource = ShipDs;
            grdShipList.DataBind();
            grdBillList.DataSource = BillDs;
            grdBillList.DataBind();


            //绑定客户联系人
            LinkManManager LinkM = new LinkManManager();
            System.Data.DataSet LinkDs = LinkM.GetListByCardCode(customer.CardCode);
            grdContactList.DataSource = LinkDs;
            grdContactList.DataBind();
        }

        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            BindData();
        }

        protected void BindResources()
        {
            //客户的用户信息
            grdUserAccountList.Columns[0].HeaderText = GetLangString("UserName");
            grdUserAccountList.Columns[1].HeaderText = GetLangString("CustomerName");
            grdUserAccountList.Columns[2].HeaderText = GetLangString("RealName");
            grdUserAccountList.Columns[3].HeaderText = GetLangString("Telephone");
            grdUserAccountList.Columns[4].HeaderText = GetLangString("RoleName");

            //shipaddress信息
            grdShipList.Columns[0].HeaderText = GetLangString("ID");
            grdShipList.Columns[1].HeaderText = GetLangString("Street");
            grdShipList.Columns[2].HeaderText = GetLangString("Block");
            grdShipList.Columns[3].HeaderText = GetLangString("ZipCode");
            grdShipList.Columns[4].HeaderText = GetLangString("City");
            grdShipList.Columns[5].HeaderText = GetLangString("County");
            grdShipList.Columns[6].HeaderText = GetLangString("Country");
            grdShipList.Columns[7].HeaderText = GetLangString("State");
            grdShipList.Columns[8].HeaderText = GetLangString("Building");

            //billaddress信息
            grdBillList.Columns[0].HeaderText = GetLangString("ID");
            grdBillList.Columns[1].HeaderText = GetLangString("Street");
            grdBillList.Columns[2].HeaderText = GetLangString("Block");
            grdBillList.Columns[3].HeaderText = GetLangString("ZipCode");
            grdBillList.Columns[4].HeaderText = GetLangString("City");
            grdBillList.Columns[5].HeaderText = GetLangString("County");
            grdBillList.Columns[6].HeaderText = GetLangString("Country");
            grdBillList.Columns[7].HeaderText = GetLangString("State");
            grdBillList.Columns[8].HeaderText = GetLangString("Building");

            //联系人信息
            grdContactList.Columns[0].HeaderText = GetLangString("LinkManID");
            grdContactList.Columns[1].HeaderText = GetLangString("CardCode");
            grdContactList.Columns[2].HeaderText = GetLangString("LMName");
            grdContactList.Columns[3].HeaderText = GetLangString("Telephone");
            grdContactList.Columns[4].HeaderText = GetLangString("Mobile");
            grdContactList.Columns[5].HeaderText = GetLangString("Fax");
            grdContactList.Columns[6].HeaderText = GetLangString("Address");
        }

        public int CustomerID
        {
            get
            {
                if (ViewState["CustomerID"] != null && TypeParse.IsNumeric(ViewState["CustomerID"]))
                    return Convert.ToInt32(ViewState["CustomerID"]);
                else
                    return 0;
            }
            set
            {
                ViewState["CustomerID"] = value;
            }
        }
    }
}
